package com.ikunikun.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ikunikun.constant.MessageConstant;
import com.ikunikun.constant.StatusConstant;
import com.ikunikun.dto.SetmealDTO;
import com.ikunikun.dto.SetmealPageQueryDTO;
import com.ikunikun.entity.Dish;
import com.ikunikun.entity.Setmeal;
import com.ikunikun.entity.SetmealDish;
import com.ikunikun.exception.SetmealEnableFailedException;
import com.ikunikun.mapper.DishMapper;
import com.ikunikun.mapper.SetmealDishMapper;
import com.ikunikun.mapper.SetmealMapper;
import com.ikunikun.result.PageResult;
import com.ikunikun.service.SetmealService;
import com.ikunikun.vo.DishItemVO;
import com.ikunikun.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 套餐服务
 */
@Slf4j
@Service
public class SetmealServiceImpl implements SetmealService {

	@Resource
	private SetmealMapper setmealMapper;
	@Resource
	private SetmealDishMapper setmealDishMapper;
	@Resource
	private DishMapper dishMapper;

	/**
	 * 新增套餐
	 * @param setmealDTO
	 */
	@Override
	@Transactional
	public void saveWithDish(SetmealDTO setmealDTO) {
		Setmeal setmeal = new Setmeal();
		BeanUtils.copyProperties(setmealDTO, setmeal);
		setmealMapper.insert(setmeal);

		// 添加套餐与菜品对应信息
		List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
		Long setmealId = setmeal.getId();
		setmealDishes = setmealDishes.stream().map(setmealDish -> {
			setmealDish.setSetmealId(setmealId);
			return setmealDish;
		}).collect(Collectors.toList());
		for (SetmealDish setmealDish : setmealDishes) {
			setmealDishMapper.insert(setmealDish);
		}
	}

	/**
	 * 套餐分页查询
	 */
	@Override
	public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
		PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
		Page<Setmeal> page = setmealMapper.pageQuery(setmealPageQueryDTO);
		long total = page.getTotal();
		List<Setmeal> result = page.getResult();
		return new PageResult(total, result);
	}

	/**
	 * 套餐状态修改
	 * @param status
	 * @param id
	 * @return
	 */
	@Override
	public void startOrStop(Integer status, Long id) {
		if (status.equals(StatusConstant.ENABLE)){
			 List<Dish> dishList = dishMapper.getBySetmealId(id);
			for (Dish dish : dishList) {
				Integer status1 = dish.getStatus();
				if (status1.equals(StatusConstant.DISABLE)){
					throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
				}
			}
		}
		Setmeal build = Setmeal.builder()
				.id(id)
				.status(status)
				.build();
		setmealMapper.update(build);
	}

	/**
	 * 根据套餐id查询套餐信息与套餐菜品对应信息
	 * @param setmealId
	 * @return
	 */
	@Override
	public SetmealVO getSetmealAndSetmealDishBySetmealId(Long setmealId) {
		SetmealVO setmealVO = new SetmealVO();
		Setmeal setmeal = setmealMapper.getById(setmealId);
		BeanUtils.copyProperties(setmeal, setmealVO);
		List<SetmealDish> setmealDish = setmealDishMapper.getBySetmealId(setmealId);
		setmealVO.setSetmealDishes(setmealDish);
		return setmealVO;
	}


	/**
	 * 修改套餐信息
	 * @param setmealDTO
	 */
	@Override
	@Transactional
	public void update(SetmealDTO setmealDTO) {
		Setmeal setmeal = new Setmeal();
		BeanUtils.copyProperties(setmealDTO, setmeal);
		setmealMapper.update(setmeal);

		Long setmealId = setmeal.getId();
		setmealDishMapper.deleteBySetmealId(setmealId);

		List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
		setmealDishes = setmealDishes.stream().map(setmealDishe -> {
			setmealDishe.setSetmealId(setmealId);
			return setmealDishe;
		}).collect(Collectors.toList());
		for (SetmealDish setmealDish : setmealDishes) {
			setmealDishMapper.insert(setmealDish);
		}
	}

	/**
	 * 批量删除套餐
	 * @param ids
	 */
	@Override
	public void delete(List<Long> ids) {
		for (Long id : ids) {
			Setmeal setmeal = setmealMapper.getById(id);
			if (Objects.equals(setmeal.getStatus(), StatusConstant.ENABLE)){
				throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ON_SALE);
			}
		}
	}

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
